import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

public class EncComm
{
	private static String replacement = "somethingthatitcantcreate";
	public static void send(PrintWriter out, Encryption enc, String data)
	{
		String encryptedData;
		encryptedData = enc.encrypt(data);
		out.println(encryptedData.getBytes().length);
		out.print(encryptedData.getBytes());
		
//		//String[] parts;
//		String encryptedData;
//		System.out.println("EncComm Send Initiated");
//		encryptedData = data.replace(' ', '&');
//		encryptedData = enc.encrypt(data);
//		System.out.println("Full encrypted data: " + encryptedData);
//		//encryptedData = encryptedData.replace("\n", replacement);
//		//parts = encryptedData.split("\n");
//		encryptedData = "";
//		for(int i=0; i<parts.length; i++)
//			encryptedData += parts[i];
//		//encryptedData = encryptedData.replaceAll("\n", replacement);
//		//System.out.println("replaced data: " + encryptedData);
//		out.println(encryptedData);
	}
	
	public static String recieve(BufferedReader in, Encryption dec)
	{
		System.out.println("EncComm recieve Initiated");
		String decryptedData = "";
		try {
			decryptedData = in.readLine();
			System.out.println("Raw read data: "+decryptedData);
			decryptedData = decryptedData.replace(replacement, "\n");
			//decryptedData = decryptedData.replaceAll(replacement, "\n");
			System.out.println("replaced read data: "+decryptedData);
			decryptedData = dec.decrypt(decryptedData);
			System.out.println("plain data: "+decryptedData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decryptedData.replace('&',' ');
	}
	
	public static String recieve(DataInputStream in, Encryption dec)
	{
		String decryptedData = "";
		int size = 0;
		byte[] bites = null;
		try {
			size = Integer.parseInt(in.readLine());
			
			bites = new byte[size];
			in.read(bites, 0, size);
			decryptedData = new String(bites);
			decryptedData = decryptedData.replaceAll(replacement, "\n");
			decryptedData = dec.decrypt(decryptedData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return decryptedData.replace('&',' ');
	}
}
